package com.penjin.android.activity.secretary;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.easemob.util.NetUtils;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.penjin.android.R;
import com.penjin.android.constants.HttpConstants;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.http.HttpService;
import com.penjin.android.service.UserService;

import org.apache.http.Header;
import org.json.JSONObject;

/**
 * Created by lxl on 2016/4/15.
 */
public class PushDetailsWebView extends Activity {

    private String id ;

    private WebView webView ;

    private String url ;

    private Context context = this ;

    private String post ;

    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what == 0 ){

                setWebView();

            }

        }
    } ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_activity_pushdetails_webview);

        id = getIntent().getStringExtra("id") ;

        post = getIntent().getStringExtra("post") ;


        webView = (WebView) findViewById(R.id.pushdetails_webview);

        init();


    }


    private void init(  ){


        HttpService httpService = HttpService.getInstance(this);
        //判断网络是否正常
        if (NetUtils.hasNetwork(this)) {
            try {

                RequestParams requestParams = new RequestParams();
                PenjinUser user =  UserService.getInstance(this).getCurrentUser() ;

                requestParams.put("companyId", user.getCompanyId());
                requestParams.put("userName", user.getPhone());
                requestParams.put("id", id);
                httpService.postRequest(this, HttpConstants.HOST + post , requestParams, new TextHttpResponseHandler() {

                    @Override
                    public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                        if (s == null) {
                            Toast.makeText(context, "服务器内部错误", Toast.LENGTH_SHORT).show();
                        } else {
                            System.out.println(s);
                            Toast.makeText(context, "资源路径出错", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onSuccess(int i, Header[] headers, String s) {
                        try {

                            Log.d("JPush",s) ;

                            JSONObject jo = new JSONObject(s);
                            if (jo.getBoolean("result")) {

                                url = jo.getString("url") ;

                            }

                            handler.sendEmptyMessage(0) ;

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "网络不可用...", Toast.LENGTH_SHORT).show();
        }

    }

    private void setWebView(){

        WebSettings webSettings = webView.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(true);
        //加载需要显示的网页
        webView.loadUrl(url);
        //设置Web视图
        webView.setWebViewClient(new webViewClient());


    }


    //Web视图
    private class webViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

}
